<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: pl125 <xskjs888@163.com>
// +----------------------------------------------------------------------

namespace api\user\controller;

use api\user\model\FeedbackModel;
use api\common\controller\RestUserBaseController;

class FeedbackController extends RestUserBaseController
{
    protected $FeedbackModel;

    public function __construct()
    {
        parent::__construct();
        $this->userId = $this->getUserId();
        $this->FeedbackModel = new FeedbackModel();
    }


    public function index()
    {

        $param          = $this->request->param();
        $param['where'] = [
            'user_id' => $this->userId
        ];
        $param['order'] = '-createtime';
        $feedbackData = $this->FeedbackModel->getDatas($param);
        $this->success('历史反馈记录', $feedbackData);
    }


    public function save()
    {
        $param = $this->request->param();
        $result          = $this->validate($param, 'Feedback');
        if ($result !== true) {
            $this->error($result);
        }
        //组装数据
        $data = $this->_FeedbackObject($param);
        if (!$data) {
            $this->error('提交失败');
        }
        if ($this->FeedbackModel->setFeedback($data)) {
            $this->success('提交成功');
        } else {
            $this->error('提交失败');
        }

    }


    protected function _FeedbackObject($param)
    {
        $data['user_id']        = $this->userId;
        $data['createtime']     = time();
        $data['feedback_content']     = $param['feedback_content'];
        $data['feedback_image']       = isset($param['feedback_image'])?$param['feedback_image']:"";;
        return $data;
    }


    public function delete()
    {
        $param          = $this->request->param();
   
       if (isset($param['id'])) {
                $count = $this->FeedbackModel->where(['id' => $param['id'], 'user_id' => $this->userId])->count();

                if ($count == 0) {
                    $this->error('数据不存在,无法删除');
                }

                $result = $this->FeedbackModel->where(['id' => $param['id']])->delete();
                if($result){
                     $this->success('删除成功');
                }else{
                      $this->error('删除失败'); 
                }
               
        } else{
            $this->error('参数错误');
        }
        
    }
}
